// vuex定义内存变量
import Vue from 'vue'
import Vuex from 'vuex'
import {
	websocketUrl
} from '../../static/js/config'
// import main from '../../utils/main'
import methods from '../../utils/methods'


Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		iosBack: false,
		deviceId: '',
		deviceModel: '',
		config: {
			host: '',
			token: '',
			websocketUrl: ''
		},
		language: 'zh',
		country: 'zh-Hans',
		osLanguage: '',
		chatDataList: '',
		userInfo: {},
		isSx: false,
		isSx2: false,
		isSxHp: false,
		isSxChatMess: false,
		platform: '',
		IntervalTime: '',
		noLike: -1,
		IntervalTimeObj: null,
		isUpdate: false,
		pkgUrl: '',
		getInfo: {
			getCode: '',
			getType: '',
			getFinish: false,
			getBean: '',
		},
		addInfo: '',
		addTopicFalg: false,
		dsform: {
			second: false,
			isClock: -1
		},
		pushNum: false,
		upvoteFalg: '',
		upvoteIndex: '',
		followFalg: -1,
		followIndex: -1,
		isOpen: false,
		SocketTask: null,
		interval: null,
		connectNum: 0,
		unreadSum: 0,
		shopUnreadSum: 0,
		userUnNum: 0,
		sessionUserIndex: -1,
		sendMessokList: '',
		sendMessokPlace: false, //外面
		sysPlace: false, //系统外面
		firstSys: '',
		clearMessageId: -1,
		samallOList: {
			// deviceName:'',
			// currentHomeId:'',
			// device:{},
			// wifiName:'',
			// wifiPass:'',
			// deviceId:''
		},
		properties: '',
		autoUpdate: '',
		laserFalg: '',
		xRprivacy: '',
		isHasFood: '',
		agreements: {},
		vipList: '',
		deviceBrand: '',
		initSmallO: false,
		updateDesc: '',
		xoIsShare: false,
		appVersion: '',
		handIsUpdate: false,
		downLoadFalg: false,
		selPetInfo: '',
		isPaySuccess: false,
		shopObj: ''
		// channel:''
	},
	mutations: {
		// 同步
		set_IsIos(state, bool) {
			state.vuexStr = bool
		}
	},
	actions: {
		link(context, index) {
			let self = this
			var msg = ''
			let token = uni.getStorageSync('token');
			if (!token) return
			//console.log('websocketUrl:',store.state.config.websocketUrl)
			console.log('websocketUrl:', websocketUrl)
			const wsurl = `${websocketUrl}`
			self.state.SocketTask = uni.connectSocket({
				url: wsurl,
				complete: (e) => {},
			});
			// 监听开启
			self.state.SocketTask.onOpen(() => {
				console.log('成功')
				self.state.connectNum = 0
				// 将连接状态设为已连接
				self.dispatch('unreadSumFun');
				self.dispatch('unreadSumShopFun');
				self.state.isOpen = true;
				//开启心跳
				// self.state.interval = setInterval(() => {
				// 	const language = uni.getStorageSync('language') || 'zh-Hans'; //获取缓存的语言设置
				// 	//发送心跳
				// 	let data = {
				// 		type: 5,
				// 		userId: self.state.userInfo.userId,
				// 		locale: language == 'zh-Hans' ? 'zh' : language
				// 	}
				// 	console.log('发送心跳:',data)

				// 	let str = JSON.stringify(data)
				// 	uni.sendSocketMessage({
				// 		data: str,
				// 		fail: function(e) {
				// 			self.state.isOpen = false;
				// 			//执行重连 
				// 			self.dispatch('reConnect');
				// 		},
				// 	});
				// }, 30000);
			});

			uni.onSocketError(function(res) {
				console.log('222')
				self.state.isOpen = false;
				self.state.SocketTask = false;
				self.dispatch('reConnect');
			});



			self.state.SocketTask.onMessage((e) => {
				if (e.data) {
					// 字符串转json
					let res = JSON.parse(e.data);
					self.state.chatDataList = res
					if (res.type != 9) {
						self.dispatch('unreadSumFun')
					} else {
						self.dispatch('unreadSumShopFun', )
					}
				}
			})
		},
		sendSocketMessageFun(arrayBuffer ) {
			let self = this
			uni.sendSocketMessage({
				data: arrayBuffer ,
				fail: function(e) {
					self.state.isOpen = false;
					//执行重连 
					self.dispatch('reConnect');
				},
			});
		},
		reConnect({
			context
		}) {
			let self = this
			clearInterval(self.state.interval)
			self.state.interval = null
			self.state.timer = setTimeout(() => {
				if (!self.state.isOpen) {
					self.dispatch('link')
				}
			}, 3000)
		},
		unreadSumFun(store) {
			methods.unreadSum().then(res => {
				if (res.data.code == 200) {
					if (res.data.data) {
						if (res.data.data.unreadSum != 0) {
							let msg = ''
							if (res.data.data.unreadSum > 99) {
								msg = '99+'
							} else {
								msg = res.data.data.unreadSum.toString()
							}
							uni.setTabBarBadge({
								index: 3,
								text: msg
							})
						} else {
							uni.removeTabBarBadge({
								index: 3
							})
						}
					}
				}
			})
		},
		unreadSumShopFun(store) {
			methods.shopUnreadSum().then(res => {
				if (res.data.code == 200) {
					if (res.data.data) {
						if (res.data.data.unreadSum != 0) {
							store.state.shopUnreadSum = res.data.data.unreadSum.toString()
						}
					}
				}
			})
		},
		time(context, data) {
			let self = this
			methods.data().then(res => {
				if (res.data.code == 200) {
					if (res.data.data) {
						let result = res.data.data.online.list
						self.state.getInfo.getType = res.data.data.online.type
						if (result[0].isGet == 0 && result[0].code == 1) {
							self.state.getInfo.getCode = result[0].code
							self.state.getInfo.getBean = result[0].bean
							if (result[0].second == 0) {
								self.state.dsform.second = true
								if (self.state.IntervalTimeObj) {
									clearInterval(self.state.IntervalTimeObj)
									self.state.IntervalTime = ''
								}
								return
							}
							if (result[0].second) {
								self.dispatch('djTime', result[0].second);
							} else {
								if (self.state.IntervalTimeObj) {
									clearInterval(self.state.IntervalTimeObj)
									self.state.IntervalTime = ''
								}
							}

							return
						}

						if (result[1].isGet == 0 && result[1].code == 2) {
							self.state.getInfo.getCode = result[1].code
							self.state.getInfo.getBean = result[1].bean
							if (result[1].second == 0) {
								self.state.dsform.second = true
								if (self.state.IntervalTimeObj) {
									clearInterval(self.state.IntervalTimeObj)
									self.state.IntervalTime = ''
								}
								return
							}

							if (result[1].second) {
								self.dispatch('djTime', result[1].second);
							} else {
								if (self.state.IntervalTimeObj) {
									clearInterval(self.state.IntervalTimeObj)
									self.state.IntervalTime = ''
								}
							}

							return
						}

						if (result[2].isGet == 0 && result[2].code == 3) {
							self.state.getInfo.getCode = result[2].code
							self.state.getInfo.getBean = result[2].bean
							if (result[2].second == 0) {
								self.state.dsform.second = true
								if (self.state.IntervalTimeObj) {
									clearInterval(self.state.IntervalTimeObj)
									self.state.IntervalTime = ''
								}
								return
							}
							if (result[2].second) {
								self.dispatch('djTime', result[2].second);
							} else {
								if (self.state.IntervalTimeObj) {
									clearInterval(self.state.IntervalTimeObj)
									self.state.IntervalTime = ''
								}
							}
							return
						}

						self.state.getInfo.getCode = 3
						self.state.getInfo.getBean = '10'
						self.state.dsform.second = false
						self.state.getInfo.getFinish = true
					}
				}
			})
		},
		djTime(context, time) {
			let self = this
			let time2 = time
			self.state.IntervalTimeObj = setInterval(() => {
				time2--
				self.state.IntervalTime = time2
				if (time2 == 0) {
					self.state.IntervalTime = ''
					self.state.dsform.second = true
					clearInterval(self.state.IntervalTimeObj)
				}
			}, 1000)
		}
	},
	getters: {

	}
})
export default store